package lec08.am;

import java.util.Iterator;
import java.util.TreeMap;

public class TreeMapDemo {
	public static void main(String[] args) {
		String[] words = "To know everything     is to? know nothing."
				.toLowerCase().split("\\W+");
		TreeMap<String, Integer> map = new TreeMap<String, Integer>();
		for (String word : words) {
			if (map.containsKey(word)) {
				int oldFreq = map.get(word);
				// map.remove(word);
				map.put(word, oldFreq + 1);
			} else {
				map.put(word, 1);
			}
		}
		System.out.println(map);
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			String curKey = it.next();
			System.out.println(String.format("%s:%d", curKey, map.get(curKey)));
		}
	}
}
